/// Create summary statistics for main text

********************************************************************************
*** load data and label variables
use "data/pseudodata/userlevel_pseudodata.dta", clear

label variable totalagentpremium "3-month premium"
label variable dayrate "Daily premium"
label variable makemodelyearvalue_private "Vehicle resale value \hangindent=1em"
label variable car_year "Vehicle year"
label variable age "Age"

label variable income_insight_score "Income Insight Score"
label variable vantage_v4_score "Vantage credit score"
label variable totinq "Total inquiries"
label variable totalrevlimit "Total revolving credit limit"
label variable cardlimit "Credit card limit"
label variable cardbalance "Credit card balance"
label variable isconstrained "Is credit constrained"
label variable autoloan "Has auto loan"
label variable autoloanamount "Auto loan amount"
label variable medcoll "Medical collections"
label variable nonmedcoll "Nonmedical collections"

label variable clar_totinq "Clarity total inquiries"
label variable clar_limits "Clarity credit limit"
label variable clar_balance "Clarity credit balance"

local pcts = "isconstrained autoloan"
foreach pct of local pcts {
	replace `pct' = `pct'*100
}

replace income_insight_score = income_insight_score*1000

********************************************************************************
*** body summstats
local hvars1 = "totalagentpremium dayrate makemodelyearvalue_private "
local hvars2 = "car_year age"
local evars = "income_insight_score vantage_v4_score totinq totalrevlimit cardlimit cardbalance isconstrained autoloan autoloanamount medcoll nonmedcoll"
local evartozero = "totinq totalrevlimit cardlimit cardbalance isconstrained autoloan autoloanamount medcoll nonmedcoll"

* Set missing Experian and Clarity variables to zeroes 
foreach var of varlist `evartozero' {
	replace `var'=0 if mi(`var')
}
local cvars = "clar_totinq clar_limits clar_balance"
foreach var of varlist `cvars' {
	replace `var'=0 if mi(`var')
}

gen in_expt = 1 if !missing(cohort)

eststo clear
keep if in_expt==1
	
eststo est1 : estpost summarize `hvars1', det
qui summ dayrate
estadd scalar n = `r(N)'
eststo est2 : estpost summarize `hvars2', det
qui summ dayrate
estadd scalar n = `r(N)'
eststo est3 : estpost summarize `evars' if !mi(totinq), det
qui summ totinq if !mi(vantage_v4_score)
estadd scalar n = `r(N)'
eststo est4 : estpost summarize `cvars' if !mi(clar_totinq), det
qui summ clar_totinq if clar_totinq!=0
estadd scalar n = `r(N)'

esttab est1 using "output/tables/summary statistics main.tex", ///
	prehead("\begin{tabular}{l*{3}{c}} \hline\hline") ///
	posthead("\hline \\ \multicolumn{4}{c}{\textit{A. Administrative data measures}} \\\\[-1ex]") ///
	replace label unstack nonumbers nomtitles ///
	fragment noobs ///
	cells("mean(fmt(%12.3gc) label(Mean)) sd(fmt(%12.3gc) label(SD)) p50(fmt(%12.3gc) label(Median))")
	
esttab est2 using "output/tables/summary statistics main.tex", ///
	fragment append ///
	prefoot("\hline") postfoot("\hline") ///
	nomtitles nonumbers nolines ///
	label unstack collabels(, none) ///
	stats(n, fmt(%11.0gc) labels("N")) ///
	cells("mean(fmt(%12.3g) label(Mean)) sd(fmt(%12.3g) label(SD)) p50(fmt(%12.3g) label(Median))")

esttab est3 using "output/tables/summary statistics main.tex", ///
	posthead(" \multicolumn{4}{c}{\textit{B. Credit report measures}} \\\\[-1ex]") ///
	fragment append ///
	nomtitles nonumbers nolines ///
	prefoot("\hline") postfoot("\hline") ///
	label unstack ///
	stats(n, fmt(%11.0gc) labels("Nonmissing credit report")) ///
	cells("mean(fmt(%12.3gc) label(\phantom{ })) sd(fmt(%12.3gc) label(\phantom{ })) p50(fmt(%12.3gc) label(\phantom{ }\vspace{-.6mm}))")
		
esttab est4 using "output/tables/summary statistics main.tex", ///
	posthead(" \multicolumn{4}{c}{\textit{C. Alternative credit report measures}} \\\\[-1ex]") ///
	fragment append ///
	nomtitles nonumbers nolines ///
	prefoot("\hline") postfoot("\hline\hline \end{tabular}") ///
	label unstack ///
	stats(n, fmt(%11.0gc) labels("Nonmissing alternative credit report")) ///
	cells("mean(fmt(%12.3gc) label(\phantom{ })) sd(fmt(%12.3gc) label(\phantom{ })) p50(fmt(%12.3gc) label(\phantom{ }\vspace{-.6mm}))")

